package com.pro.service.imple;

import com.pro.mapper.JsCourseClassMapper;
import com.pro.pojo.JsCourse;
import com.pro.pojo.JsCourseClass;
import com.pro.pojo.JsonData;
import com.pro.service.JsCarouselService;
import com.pro.service.JsCourseClassService;
import com.pro.utils.DateUtils;
import com.pro.utils.RedisCatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author 祖铭泽
 * @Date 2022/1/12/0012
 * @description 课程分类 业务层实现类
 */
@Service
public class JsCourseClassServiceImple implements JsCourseClassService {
    @Autowired
    private JsCourseClassMapper jsCourseClassMapper;
    @Autowired
    private RedisCatch redisCatch;
    /**
     * @Author 祖铭泽
     * @Date 2022/1/12/0012
     * @description 查询所有课程分类
     */
    @Override
    public JsonData getAllJsCourseClassSelect() {
        List<JsCourseClass> jsCourseClasses = ifRedisCacheHaveTree("courseClass");
        return jsCourseClasses != null ? JsonData.buildSuccess(jsCourseClasses) : JsonData.buildError("查询失败");
    }
    /**
     * @Author 祖铭泽
     * @Date 2022/1/12/0012
     * @description 查询所有课程分类（树结构）
     */
    @Override
    public JsonData getAllJsCourseClassTree() {
        List<JsCourseClass> jsCourseClasses = ifRedisCacheHaveTree("courseClassTree");
        jsCourseClasses.add(new JsCourseClass(0,"选择分类类型",-1));
        return jsCourseClasses != null ? JsonData.buildSuccess(jsCourseClasses) : JsonData.buildError("查询失败");
    }
    /**
     * @Author 祖铭泽
     * @Date 2022/2/9/0012
     * @description 判断Redis缓存中是否有数据
     */
    private List<JsCourseClass> ifRedisCacheHaveTree(String cacheName){
        List<JsCourseClass> courseClassTree = redisCatch.getCacheList(cacheName);
        if(courseClassTree == null || courseClassTree.isEmpty()){
            courseClassTree = jsCourseClassMapper.getAllJsCourseClass();
            redisCatch.setCacheList(cacheName,courseClassTree);
        }
        return courseClassTree;
    }
    /**
     * @Author 祖铭泽
     * @Date 2022/1/13/0012
     * @description 添加课程分类
     */
    @Override
    public JsonData addCourseClass(JsCourseClass jsCourseClass) {
        jsCourseClass.setCreateDate(DateUtils.tranferDateTimeToString(new Date()));
        boolean addCourseClass = jsCourseClassMapper.addCourseClass(jsCourseClass);
        boolean courseClassTree = redisCatch.deleteObject("courseClassTree");
        return addCourseClass && courseClassTree ? JsonData.buildSuccess(jsCourseClass) : JsonData.buildError("添加失败");
    }
    /**
     * @Author 祖铭泽
     * @Date 2022/1/13/001
     * @description 删除课程分类
     */
    @Override
    public JsonData deleteCourseClass(Integer id) {
        boolean deleteCourseClass = jsCourseClassMapper.deleteCourseClass(id);
        boolean courseClassTree = redisCatch.deleteObject("courseClassTree");
        return deleteCourseClass && courseClassTree ? JsonData.buildSuccess(deleteCourseClass) : JsonData.buildError("删除失败");
    }
    /**
     * @Author 祖铭泽
     * @Date 2022/1/13/001
     * @description 修改课程分类
     */
    @Override
    public JsonData updateCourseClass(JsCourseClass jsCourseClass) {
        jsCourseClass.setUpdateDate(DateUtils.tranferDateTimeToString(new Date()));
        boolean updateCourseClass = jsCourseClassMapper.updateCourseClass(jsCourseClass);
        boolean courseClassTree = redisCatch.deleteObject("courseClassTree");
        return updateCourseClass && courseClassTree ? JsonData.buildSuccess(updateCourseClass) : JsonData.buildError("修改失败");
    }
}
